package com.vic.delayedqueue.controller;

import com.vic.delayedqueue.config.DelayedQueue2Config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * 通用的延时队列
 */
@Slf4j
@RestController
@RequestMapping("producer2")
public class Producer2Controller {

    @Autowired
    RabbitTemplate rabbitTemplate;

    /**
     * 消息的过期时间由参数传入
     * @param message
     * @param ttlTime
     * @return
     */
    @GetMapping("/ttl/sendMsg/{message}/{ttlTime}")
    public String sendExpirationMsg(@PathVariable String message, @PathVariable String ttlTime) {
        log.info("当前时间: {},发送一条时长 {} 毫秒 TTL 信息给队列: {}", new Date(), ttlTime, message);
        // 设置延时时间
        rabbitTemplate.convertAndSend(DelayedQueue2Config.COMMON_EXCHANGE, DelayedQueue2Config.COMMON_ROUTING_KEY, message, messagePostProcessor -> {
            messagePostProcessor.getMessageProperties().setExpiration(ttlTime);
            return messagePostProcessor;
        });
        return "ok";
    }

}
